package formula.business.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Version;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
public class Team implements Serializable {

	private static final long serialVersionUID = -2898506344162047530L;
	private Long id;
	private int version;
	private String name;
	private List<Driver> drivers;

	@GenericGenerator(strategy = "org.hibernate.id.enhanced.TableGenerator", name = "teamGen", parameters = {
			@Parameter(name = "table_name", value = "ID_GENERATOR"),
			@Parameter(name = "value_column_name", value = "GEN_VALUE"),
			@Parameter(name = "segment_column_name", value = "GEN_ID"),
			@Parameter(name = "segment_value", value = "TEAM_ID"), @Parameter(name = "increment_size", value = "50"),
			@Parameter(name = "optimizer", value = "pooled") })
	@Id
	@Column(name = "id", unique = true, nullable = false)
	@GeneratedValue(generator = "teamGen")
	public Long getId() {
		return id;
	}

	public Team() {
	}

	public Team(String _name) {
		name = _name;
	}

	public void setId(Long _id) {
		id = _id;
	}

	@Version
	public int getVersion() {
		return version;
	}

	public void setVersion(int _version) {
		version = _version;
	}

	public String getName() {
		return name;
	}

	public void setName(String _name) {
		name = _name;
	}

	@OneToMany(mappedBy = "team")
	public List<Driver> getPilots() {
		return drivers;
	}

	public void setPilots(List<Driver> _pilots) {
		drivers = _pilots;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Team other = (Team) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}
